package com.luhuiguo.cms.server.web;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.luhuiguo.cms.server.entity.Attachment;
import com.luhuiguo.cms.server.service.AttachmentService;

@Controller
public class DownloadController {

    @Autowired
    private AttachmentService attachmentService;

    @RequestMapping(value = "/downloads/{id}/*")
    public ModelAndView handleDownload(@PathVariable("id") Long id) {

        ModelAndView mav = new ModelAndView("download");

        Attachment attachment = attachmentService.getAttachment(id);

        mav.addObject("attachment", attachment);

        return mav;
    }

    @RequestMapping(value = "/attachments/{id}/*")
    public ModelAndView handleAttachment(@PathVariable("id") Long id) {

        ModelAndView mav = new ModelAndView("attachment");

        Attachment attachment = attachmentService.getAttachment(id);

        mav.addObject("attachment", attachment);

        return mav;
    }
}
